package com.example.spring.jpa.ast.operation;

import com.example.spring.jpa.ast.AstExpression;
import com.example.spring.jpa.ast.AstNode;
import com.example.spring.jpa.ast.AstStatic;

/**
 * @author: orange
 * @create: 2023-07-23 18:00
 * @description:
 */
public class AstBracket implements AstGrammar {
    private String txt;
    private AstGrammar astGrammar;

    private AstBracket() {}
    public AstBracket(String content) {
        this.txt = content;
    }

    @Override
    public AstNode parse() throws Exception {
        AstExpression expression = new AstExpression(sentence());
        AstNode astNode = expression.parse();
        if (astNode instanceof AstGrammar) {
            this.astGrammar = (AstGrammar) astNode;
            this.setTxt(astNode.sentence());
        } else if (astNode instanceof AstStatic) {
            return new AstStatic(astNode.sentence());
        }
        return this;
    }

    @Override
    public String sentence() {
        return this.txt;
    }

    @Override
    public AstNode getLeft() {
        return this.astGrammar.getLeft();
    }

    @Override
    public AstNode getRight() {
        return this.astGrammar.getRight();
    }

    public void setTxt(String txt) {
        this.txt = txt;
    }

    public AstGrammar getAstGrammar() {
        return astGrammar;
    }
}
